package com.zhongxiangspace.tools;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;


public class MessageCaptchaUtil {

    /**
     * 短信账户
     */
    public final static String MESSAGE_ACCOUNT = "CQLKY00706";
    /**
     * 短信账户密码
     */
    public final static String MESSAGE_PASSWORD = "CQLKY00706@@";

    /**
     * 发送方法
     *
     * @param Mobile    发送手机号
     * @param Content   发送的内容
     * @param send_time 发送时间
     * @return
     * @throws MalformedURLException
     * @throws UnsupportedEncodingException
     */
    public static int sendSMS(String Mobile, String Content, String send_time) throws MalformedURLException, UnsupportedEncodingException {
        URL url = null;
        String CorpID = MESSAGE_ACCOUNT;//账户名
        String Pwd = MESSAGE_PASSWORD;//密码
        String send_content = URLEncoder.encode(Content.replaceAll("<br/>", ""), "GBK");//发送内容
        url = new URL("http://yzm.mb345.com/ws/BatchSend2.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + "&Mobile=" + Mobile + "&Content=" + send_content + "&Cell=&SendTime=" + send_time);
        BufferedReader in = null;
        int inputLine = 0;
        try {
            System.out.println("开始发送短信手机号码为 ：" + Mobile);
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            inputLine = new Integer(in.readLine()).intValue();
        } catch (Exception e) {
            System.out.println("网络异常,发送短信失败！");
            inputLine = -2;
        }
        System.out.println("结束发送短信返回值：  " + inputLine);
        return inputLine;
    }
    public final static String phone = "18580100909";
    public static boolean sund(String content) {
        boolean falg = false;
        content = "开发测试!有客户"+content+"预留信息,请查看!预留时间:" + DateUtil.getDateTime();
        try {
            int i = MessageCaptchaUtil.sendSMS(phone, content, "");
            if (i == -2) {
                falg = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            falg = false;
        }
        return falg;
    }
}
